package edu.ccut.saturn.component.impl;

import edu.ccut.saturn.component.IDataItem;
import edu.ccut.saturn.component.IValidateUseInfo;
import edu.ccut.saturn.component.loader.SaturnDataLoader;
import edu.ccut.saturn.component.xml.ISaturnElement;
import edu.ccut.saturn.component.xml.SaturnElement;

/**
 * Record a Data information
 * 
 * @author LeslieGu
 * 
 */
public class SaturnDataItem implements IDataItem {

	private String name = "";

	private String type = "";

	private String column = "";

	private String columnAlias = "";

	private String isPrimaryKey = "false";

	private String sequenceName = "";

	private String javaType;

	private String tableName = "";

	private String validateKey = "";

	private String showName = "";

	private String defaultValue = "";

	private String dictTypeId = "";

	private IValidateUseInfo validateUseInfo;

	private String length = "";

	private String datetimeFormat = "";

	private String numberFormat = "";

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.ccut.saturn.component.impl.IDataInfo#getName()
	 */
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.ccut.saturn.component.impl.IDataInfo#getType()
	 */
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.ccut.saturn.component.impl.IDataInfo#getColume()
	 */
	public String getColumn() {
		return column;
	}

	public void setColumn(String colume) {
		this.column = colume;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.ccut.saturn.component.impl.IDataInfo#getColumeAlias()
	 */
	public String getColumnAlias() {
		return columnAlias;
	}

	public void setColumnAlias(String columeAlias) {
		this.columnAlias = columeAlias;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.ccut.saturn.component.impl.IDataInfo#getIsPrimaryKey()
	 */
	public String getIsPrimaryKey() {
		return isPrimaryKey;
	}

	public void setIsPrimaryKey(String isPrimaryKey) {
		this.isPrimaryKey = isPrimaryKey;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.ccut.saturn.component.impl.IDataInfo#getSequenceName()
	 */
	public String getSequenceName() {
		return sequenceName;
	}

	public void setSequenceName(String sequenceName) {
		this.sequenceName = sequenceName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.ccut.saturn.component.impl.IDataInfo#getJavaType()
	 */
	public String getJavaType() {
		return javaType;
	}

	public void setJavaType(String javaType) {
		this.javaType = javaType;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.ccut.saturn.component.impl.IDataInfo#getTableName()
	 */
	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getDefaultValue() {
		return this.defaultValue;
	}

	public String getValidateKey() {
		return validateKey;
	}

	public void setValidateKey(String validateKey) {
		this.validateKey = validateKey;
	}

	public String getShowName() {
		return showName;
	}

	public void setShowName(String showName) {
		this.showName = showName;
	}

	public void setDefaultValue(String defauleValue) {
		this.defaultValue = defauleValue;
	}

	public String getDictTypeId() {
		return dictTypeId;
	}

	public void setDictTypeId(String dictTypeId) {
		this.dictTypeId = dictTypeId;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {

		if (this == obj) {
			return true;
		}

		if (obj == null) {
			return false;
		}

		if (getClass() != obj.getClass()) {
			return false;
		}

		final SaturnDataItem other = (SaturnDataItem) obj;

		if (name == null) {

			if (other.name != null) {
				return false;
			}

		} else if (!name.equals(other.name)) {
			return false;
		}
		return true;
	}

	// TODO:完善内容
	public String toString() {
		return "[name=" + this.name + ", javaType=" + this.javaType
				+ ", column=" + this.column + ", columeAlias="
				+ this.columnAlias + "]";
	}

	public IValidateUseInfo getValidateUseInfo() {
		return this.validateUseInfo;
	}

	public void setValidateUseInfo(IValidateUseInfo validateUseInfo) {
		this.validateUseInfo = validateUseInfo;
	}

	public String getLength() {
		return this.length;
	}

	public void setLength(String length) {
		this.length = length;
	}

	public String getDatetimeFormat() {
		return datetimeFormat;
	}

	public void setDatetimeFormat(String datetimeFormat) {
		this.datetimeFormat = datetimeFormat;
	}

	public String getNumberFormat() {
		return numberFormat;
	}

	public void setNumberFormat(String numberFormat) {
		this.numberFormat = numberFormat;
	}

	public ISaturnElement toSaturnElement() {
		ISaturnElement fieldElement = new SaturnElement(
				SaturnDataLoader.KEY_SATURNDATA_FIELDS_FIELD);

		fieldElement.addAttribute(SaturnDataLoader.KEY_DATA_FIELD_COLUMN,
				getColumn());

		fieldElement.addAttribute(
				SaturnDataLoader.KEY_DATA_FIELD_DATETIME_FORMAT,
				getDatetimeFormat());

		fieldElement.addAttribute(SaturnDataLoader.KEY_DATA_FIELD_DEFAULTVALUE,
				getDefaultValue());

		fieldElement.addAttribute(SaturnDataLoader.KEY_DATA_FIELD_DICTTYPEID,
				getDictTypeId());

		fieldElement.addAttribute(SaturnDataLoader.KEY_DATA_FIELD_JAVATYPE,
				getJavaType());

		fieldElement.addAttribute(SaturnDataLoader.KEY_DATA_FIELD_LENGTH,
				getLength());

		fieldElement.addAttribute(SaturnDataLoader.KEY_DATA_FIELD_NAME,
				getName());

		fieldElement.addAttribute(SaturnDataLoader.KEY_DATA_FIELD_NUMBER_FORMAT,
				getNumberFormat());

		fieldElement.addAttribute(SaturnDataLoader.KEY_DATA_FIELD_SHOWNAME,
				getShowName());

		fieldElement.addAttribute(SaturnDataLoader.KEY_DATA_FIELD_TYPE,
				getType());

		fieldElement.addAttribute(SaturnDataLoader.KEY_SATURNDATA_COLUMNALIAS,
				getColumnAlias());

		fieldElement.addAttribute(SaturnDataLoader.KEY_SATURNDATA_ISPRIMARYKEY,
				getIsPrimaryKey());

		fieldElement.addAttribute(SaturnDataLoader.KEY_SATURNDATA_SEQUENCENAME,
				getSequenceName());

		fieldElement.addAttribute(SaturnDataLoader.KEY_SATURNDATA_TABLENAME,
				getTableName());

		if (this.validateUseInfo != null) {
			fieldElement.addSubElement(this.validateUseInfo.toSaturnElement());
		}
		return fieldElement;
	}

	public String toXmlStr() {
		return toSaturnElement().getXMLStr();
	}
}
